<?php
declare (strict_types=1);

namespace app\admin\controller\iot;

use app\admin\model\iot\IntegralTask as IntegralTaskModel;
use app\admin\model\iot\IntegralTaskComplet;
use app\admin\validates\IntegralTaskValidate;
use app\BaseController;
use think\exception\ValidateException;
use think\facade\Request;
use think\response\Json;
use app\advertiser\service\AdMatterService;

/**
 * 积分任务
 */
class IntegralTask extends BaseController
{

    /**
     * 积分任务列表
     * @return Json
     */
    public function list(): Json
    {
        $params['name']=Request::param('name/s');
        $params['type']=Request::param('type/d');
        $params['enabled']=Request::param('enabled/d');
        $params['page']=Request::param('page/d',1);
        $params['page_size']=Request::param('page_size/d',10);
        $where =[];
        if ($params['name']) {
            $where[] = ['name', 'like', '%' . $params['name'] . '%'];
        }
        //搜索类型
        if ($params['type'] and in_array($params['type'], [1, 2])) {
            $where[] = ['type', '=', $params['type']];
        }
        //搜索状态
        if (isset($params['enabled'])) {
            $where[] = ['enabled', '=', $params['enabled']];
        }
        $data = IntegralTaskModel::getList($where,$params['page'],$params['page_size']);
        return jsonReturn(1, $data);
    }

    /**
     * 积分任务新增
     * @return Json
     */
    public function add(): Json
    {
        $params['name'] = Request::param('name/s');
        $params['type'] = Request::param('type/d');
        $params['bonus_points'] = Request::param('bonus_points/d');
        $params['jump_url'] = Request::param('jump_url/s');
        $params['mini_jump_url'] = Request::param('mini_jump_url/s');
        $params['official_id'] = Request::param('official_id/s');
        $params['enabled'] = Request::param('enabled/d',0);
        //数据验证
        try {
            validate(IntegralTaskValidate::class)->scene('add')->check($params);
        } catch (ValidateException $e) {
            return jsonReturn(0, [], $e->getError());
        }
        switch ($params['type']){
            case 1:
            case 4:
            case 5:
                $params['num'] = count(explode($params['jump_url'],","))??0;
                break;
            case 2:
                $params['num'] = count(explode($params['official_id'],","))??0;
                break;
            case 3:
                $params['num'] = count(explode($params['mini_jump_url'],","))??0;
                break;
            default:
                break;
        }
        $res = (new IntegralTaskModel())->save($params);
        if ($res) {
            return jsonReturn(1, [], '新增成功');
        }
        return jsonReturn(0, [], '新增失败');
    }

    /**
     * 积分任务编辑
     * @return Json
     */
    public function edit(): Json
    {
        $params['id'] = Request::param('id/d');
        $params['name'] = Request::param('name/s');
        $params['type'] = Request::param('type/d');
        $params['bonus_points'] = Request::param('bonus_points/d');
        $params['jump_url'] = Request::param('jump_url/s');
        $params['mini_jump_url'] = Request::param('mini_jump_url/s');
        $params['official_id'] = Request::param('official_id/s');
        $params['enabled'] = Request::param('enabled/d',0);
        //数据验证
        try {
            validate(IntegralTaskValidate::class)->scene('edit')->check($params);
        } catch (ValidateException $e) {
            return jsonReturn(0, [], $e->getError());
        }
        //获取当前模型
        $Model = IntegralTaskModel::where(['id' => $params['id']])->find();
        if (!$Model) {
            return jsonReturn(0, [], '对象不存在');
        }
        switch ($params['type']){
            case 1:
            case 4:
            case 5:
                $params['num'] = count(explode(",",$params['jump_url']))??0;
                break;
            case 2:
                $params['num'] = count(explode(",",$params['official_id']))??0;
                break;
            case 3:
                $params['num'] = count(json_decode($params['mini_jump_url'],true))??0;
                break;
            default:
                break;
        }
        $res = $Model->save($params);
        if ($res) {
            return jsonReturn(1, [], '修改成功');
        }
        return jsonReturn(0, [], '修改失败');
    }

    /**
     * 积分任务上下架
     * @return Json
     */
    public function taskEnable(): Json
    {
        $params['id'] = Request::param('id/d', 0);
        $params['enabled'] = Request::param('enabled/d', 0);
        //数据验证
        try {
            validate(IntegralTaskValidate::class)->scene('enabled')->check($params);
        } catch (ValidateException $e) {
            return jsonReturn(0, [], $e->getError());
        }
        //获取当前模型
        $Model = IntegralTaskModel::where(['id' => $params['id']])->find();
        if (!$Model) {
            return jsonReturn(0, [], '对象不存在');
        }
        $res = $Model->save($params);
        if ($res) {
            return jsonReturn(1, [], '操作成功');
        }
        return jsonReturn(0, [], '操作失败');

    }

    /**
     * 获取推广物列表
     * @param AdMatterService $adMatterService
     * @return Json
     */
    public function MatterList(AdMatterService $adMatterService){
        $params['page'] = Request::param('page/d', 1);
        $params['limit'] = Request::param('limit/d', 10);
        $params['name'] = Request::param('name/s');
        $params['type'] = Request::param('type/s',1);
        $params['start_time'] = Request::param('start_time/d', 0);
        $params['end_time'] = Request::param('end_time/d', 0);
        return $adMatterService->list($params,$params['type']);
    }

    public static function taskStatistics(){

        $params['task_id'] = Request::param('task_id', 0);
        $where =[];
        if($params['task_id']){
            $where[]=['task_id','=',$params['task_id']];
        }
        $task_complet = IntegralTaskComplet::getListGroupPage($where,"FROM_UNIXTIME(create_time,'%Y-%m-%d'),task_id","count(1) as num,FROM_UNIXTIME(create_time,'%Y-%m-%d') as date,task_id","FROM_UNIXTIME(create_time,'%Y-%m-%d')");
        return jsonReturn(1, $task_complet );
    }













}
